package handler

import (
	"fitness/go-admin/internal/model"
	"fitness/go-admin/internal/service"
	"fitness/go-admin/pkg/response"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type DictHandler struct {
	dictService *service.DictService
}

func NewDictHandler(dictService *service.DictService) *DictHandler {
	return &DictHandler{dictService: dictService}
}

// DictType 相关接口

// CreateDictType 创建字典类型
func (h *DictHandler) CreateDictType(c *gin.Context) {
	var req struct {
		Name   string `json:"name" binding:"required"`
		Type   string `json:"type" binding:"required"`
		Status int    `json:"status" binding:"required"`
		Remark string `json:"remark"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数验证失败: "+err.Error())
		return
	}

	dictType := &model.DictType{
		Name:   req.Name,
		Type:   req.Type,
		Status: req.Status,
		Remark: req.Remark,
	}

	if err := h.dictService.CreateDictType(dictType); err != nil {
		response.Error(c, http.StatusInternalServerError, "创建字典类型失败: "+err.Error())
		return
	}

	response.Success(c, dictType, "创建字典类型成功")
}

// GetDictType 获取字典类型详情
func (h *DictHandler) GetDictType(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误")
		return
	}

	dictType, err := h.dictService.GetDictTypeByID(uint(id))
	if err != nil {
		response.Error(c, http.StatusNotFound, "字典类型不存在")
		return
	}

	response.Success(c, dictType, "获取字典类型成功")
}

// ListDictTypes 获取字典类型列表
func (h *DictHandler) ListDictTypes(c *gin.Context) {
	name := c.Query("name")
	dictType := c.Query("type")
	status, _ := strconv.Atoi(c.Query("status"))
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	dictTypes, total, err := h.dictService.ListDictTypes(name, dictType, status, page, pageSize)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "获取字典类型列表失败: "+err.Error())
		return
	}

	response.Success(c, gin.H{
		"list":     dictTypes,
		"total":    total,
		"page":     page,
		"pageSize": pageSize,
	}, "获取字典类型列表成功")
}

// UpdateDictType 更新字典类型
func (h *DictHandler) UpdateDictType(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误")
		return
	}

	var req struct {
		Name   string `json:"name"`
		Type   string `json:"type"`
		Status int    `json:"status"`
		Remark string `json:"remark"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数验证失败: "+err.Error())
		return
	}

	dictType, err := h.dictService.GetDictTypeByID(uint(id))
	if err != nil {
		response.Error(c, http.StatusNotFound, "字典类型不存在")
		return
	}

	// 更新字段
	if req.Name != "" {
		dictType.Name = req.Name
	}
	if req.Type != "" {
		dictType.Type = req.Type
	}
	if req.Status != 0 {
		dictType.Status = req.Status
	}
	dictType.Remark = req.Remark

	if err := h.dictService.UpdateDictType(dictType); err != nil {
		response.Error(c, http.StatusInternalServerError, "更新字典类型失败: "+err.Error())
		return
	}

	response.Success(c, dictType, "更新字典类型成功")
}

// DeleteDictType 删除字典类型
func (h *DictHandler) DeleteDictType(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误")
		return
	}

	if err := h.dictService.DeleteDictType(uint(id)); err != nil {
		response.Error(c, http.StatusInternalServerError, "删除字典类型失败: "+err.Error())
		return
	}

	response.Success(c, nil, "删除字典类型成功")
}

// DictData 相关接口

// CreateDictData 创建字典数据
func (h *DictHandler) CreateDictData(c *gin.Context) {
	var req struct {
		DictTypeID uint   `json:"dictTypeId" binding:"required"`
		Label      string `json:"label" binding:"required"`
		Value      string `json:"value" binding:"required"`
		Sort       int    `json:"sort"`
		Status     int    `json:"status" binding:"required"`
		Remark     string `json:"remark"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数验证失败: "+err.Error())
		return
	}

	dictData := &model.DictData{
		DictTypeID: req.DictTypeID,
		Label:      req.Label,
		Value:      req.Value,
		Sort:       req.Sort,
		Status:     req.Status,
		Remark:     req.Remark,
	}

	if err := h.dictService.CreateDictData(dictData); err != nil {
		response.Error(c, http.StatusInternalServerError, "创建字典数据失败: "+err.Error())
		return
	}

	response.Success(c, dictData, "创建字典数据成功")
}

// GetDictData 获取字典数据详情
func (h *DictHandler) GetDictData(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误")
		return
	}

	dictData, err := h.dictService.GetDictDataByID(uint(id))
	if err != nil {
		response.Error(c, http.StatusNotFound, "字典数据不存在")
		return
	}

	response.Success(c, dictData, "获取字典数据成功")
}

// ListDictData 获取字典数据列表
func (h *DictHandler) ListDictData(c *gin.Context) {
	dictTypeID, _ := strconv.ParseUint(c.Query("dictTypeId"), 10, 64)
	label := c.Query("label")
	value := c.Query("value")
	status, _ := strconv.Atoi(c.Query("status"))
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	dictData, total, err := h.dictService.ListDictData(uint(dictTypeID), label, value, status, page, pageSize)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "获取字典数据列表失败: "+err.Error())
		return
	}

	response.Success(c, gin.H{
		"list":     dictData,
		"total":    total,
		"page":     page,
		"pageSize": pageSize,
	}, "获取字典数据列表成功")
}

// GetDataByDictType 根据字典类型获取字典数据
func (h *DictHandler) GetDataByDictType(c *gin.Context) {
	dictType := c.Param("type")
	if dictType == "" {
		response.Error(c, http.StatusBadRequest, "字典类型不能为空")
		return
	}

	dictData, err := h.dictService.GetDataByDictType(dictType)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "获取字典数据失败: "+err.Error())
		return
	}

	response.Success(c, dictData, "获取字典数据成功")
}

// UpdateDictData 更新字典数据
func (h *DictHandler) UpdateDictData(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误")
		return
	}

	var req struct {
		DictTypeID uint   `json:"dictTypeId"`
		Label      string `json:"label"`
		Value      string `json:"value"`
		Sort       int    `json:"sort"`
		Status     int    `json:"status"`
		Remark     string `json:"remark"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数验证失败: "+err.Error())
		return
	}

	dictData, err := h.dictService.GetDictDataByID(uint(id))
	if err != nil {
		response.Error(c, http.StatusNotFound, "字典数据不存在")
		return
	}

	// 更新字段
	if req.DictTypeID != 0 {
		dictData.DictTypeID = req.DictTypeID
	}
	if req.Label != "" {
		dictData.Label = req.Label
	}
	if req.Value != "" {
		dictData.Value = req.Value
	}
	dictData.Sort = req.Sort
	if req.Status != 0 {
		dictData.Status = req.Status
	}
	dictData.Remark = req.Remark

	if err := h.dictService.UpdateDictData(dictData); err != nil {
		response.Error(c, http.StatusInternalServerError, "更新字典数据失败: "+err.Error())
		return
	}

	response.Success(c, dictData, "更新字典数据成功")
}

// DeleteDictData 删除字典数据
func (h *DictHandler) DeleteDictData(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "参数错误")
		return
	}

	if err := h.dictService.DeleteDictData(uint(id)); err != nil {
		response.Error(c, http.StatusInternalServerError, "删除字典数据失败: "+err.Error())
		return
	}

	response.Success(c, nil, "删除字典数据成功")
}

// BatchDeleteDictData 批量删除字典数据
func (h *DictHandler) BatchDeleteDictData(c *gin.Context) {
	var req struct {
		Ids []uint `json:"ids" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数验证失败: "+err.Error())
		return
	}

	if err := h.dictService.BatchDeleteDictData(req.Ids); err != nil {
		response.Error(c, http.StatusInternalServerError, "批量删除字典数据失败: "+err.Error())
		return
	}

	response.Success(c, nil, "批量删除字典数据成功")
}
